<?php
/**
 * Created by PhpStorm.
 * User: yu
 * Date: 16-10-31
 * Time: 上午10:43
 */

namespace index\action;

use herosphp\http\HttpRequest;
use herosphp\utils\AjaxResult;
use herosphp\bean\Beans;
use herosphp\string\StringUtils;
use Symfony\Component\DependencyInjection\Tests\Compiler\H;

/**--tmpl-use--**/

class IndexAction extends CommonAction
{
    protected $brandBean="information.brand.service";


    public function index(){
        parent::index();
        $adLeft = $this->adService->getItems(array("ad_place_key"=>"587a14490edf0e18d2a0995e13b0de20"),array(),array(),3);
        $notices = Beans::get("information.informationNotice.service")->getItem(array('release_status'=>1),array(),array('addtime'=>-1));
        $products=$this->getProducts();
        //是否开启验证码验证
        $settingService = Beans::get('admin.adminSetting.service');
        $verifyCodeEnable = $settingService->getSetting('verifyCode');
        $this->assign('verifyCodeEnable',$verifyCodeEnable);
        $this->assign('notice',$notices);
        $this->assign('adLeft',$adLeft);
        $this->assign('proLists',$products);
        $this->setView('index_start');
    }
    public function start(){
        $this->setView('index_start');
    }
    public function debug() {/*用于调试页面*/

        $this->display("/debug");
    }
    public function error() {/*显示404页面*/

        $this->display("/404");
    }

    public function getProducts()
    {
        $typeName = $this->getTypeName();
        foreach($typeName as $key=>$item)
        {
            //只查询一种品名的产品，其他通过后续调取
            $pro = $this->ProService->search(5,1,array('product_name'=>$item['proName'][0]['product_name'],'type_name'=>$item['type_name'],'status'=>1));//
            $typeName[$key]['proList'] = $pro['items'];
        }
        return $typeName;
    }
    /**
     * 此方法应该在服务层，暂时放控制器中
     * return @typeName array 包含了一个二维数组
    */
    public function getTypeName()
    {
        $typeName = $this->ProService->field("DISTINCT type_name")->where(array('status'=>1))->find();
        foreach ($typeName as $key => $item) {
           // $this->ProService->
            $proName = $this->ProService->field("DISTINCT product_name")->limit(1,7)->where(array('type_name'=>$item['type_name'],'status'=>1))->find();
            $brand = Beans::get($this->brandBean)->getItems(array('cate'=>$item['type_name'],'status'=>1),array(),array('listorder'=>1),5);
            $typeName[$key]['proName'] = $proName;
            $typeName[$key]['brand'] = $brand;
        }
        return $typeName;
    }

    //传输给前端的json数据包
    public function productsToJson(HttpRequest $request)
    {
        $type_name = $request->getParameter('type_name');
        $pro_name = $request->getParameter('pro_name');
        $items = $this->ProService->search(5,1,array('product_name'=>$pro_name,'type_name'=>$type_name));
        $pro = $items['items'];
        if($pro) {
            AjaxResult::ajaxResult("000", "获取数据成功", $pro);
        }
        else{
            AjaxResult::ajaxResult("001","获取数据失败");
        }
    }
}